package com.threeti.mecool.core.application.basicdata;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.threeti.jiayusms.framework.util.Lists;
import com.threeti.mecool.core.domain.model.acl.Orgnization;
import com.threeti.mecool.core.domain.model.basicdata.Brand;
import com.threeti.mecool.core.domain.model.basicdata.Customer;
import com.threeti.mecool.core.domain.model.basicdata.Product;
import com.threeti.mecool.core.domain.model.basicdata.biz.Channel;
import com.threeti.mecool.core.domain.model.basicdata.biz.ChannelCategory;
import com.threeti.mecool.core.domain.model.basicdata.biz.ChannelSysCategory;
import com.threeti.mecool.core.util.TreeHelper;
import com.threeti.mecool.legacy.domain.model.acl.OrgnizationLegacy;
import com.threeti.mecool.legacy.domain.model.basicdata.BrandLegacy;
import com.threeti.mecool.legacy.domain.model.basicdata.CustomerLegacy;
import com.threeti.mecool.legacy.domain.model.basicdata.ProductLegacy;
import com.threeti.mecool.legacy.domain.model.basicdata.biz.PosKindLegacy;
import com.threeti.mecool.legacy.domain.model.basicdata.biz.PosLegacy;
import com.threeti.mecool.legacy.domain.model.basicdata.biz.PosSysTypeLegacy;
import com.threeti.mecool.legacy.infrastructure.persistence.AdminDivisionLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.BrandLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.CustomerLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.OrgnizationLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.PosKindLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.PosLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.PosSysTypeLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.ProductLegacyRepository;

@Service
public class BasicDataServiceImpl implements BasicDataService {
  private static final Logger logger = LoggerFactory.getLogger(BasicDataServiceImpl.class);
  
  @Autowired
  AdminDivisionLegacyRepository adminDivLegacyRepository;
  
  @Autowired
  PosKindLegacyRepository posKindLegacyRepo;
  
  @Autowired
  PosLegacyRepository posLegacyRepo;
  
  @Autowired
  PosSysTypeLegacyRepository posSysTypeLegacyRepository;
  
  @Autowired
  CustomerLegacyRepository customerLegacyRepo;
  
  @Autowired
  ProductLegacyRepository productLegacyRepo;
  
  @Autowired
  BrandLegacyRepository brandLegacyRepo;

//  @Override
//  public List<AdminDivision> findAllAdminDivision() {
//    //1.build tree's fast image
//    List<AdminDivisionLegacy> adminDivs = adminDivLegacyRepository.findAll();
//
//    Map<String, LinkedHashSet<Long>> treeFastImage = new HashMap<String, LinkedHashSet<Long>>();
//    List<Long> topLevelNodeIds = new ArrayList<Long>();
//    
//    for (AdminDivisionLegacy adminDiv : adminDivs) {
//      Long currentNodeId = adminDiv.getId();
//      String currentParentCity = adminDiv.getParentCityAdName();
//      String currentParentProvince = adminDiv.getParentProvinceAdName();
//      String currentParentId3 = adminDiv.getName();
//      
//      if (StringUtils.isEmpty(currentParentCity) && StringUtils.isEmpty(currentParentProvince)) {
//        topLevelNodeIds.add(currentNodeId);//store all the top level adminDivs
//      } else {
//        LinkedHashSet<Long> currentChildIds = null;
//        if ((currentChildIds = treeFastImage.get(currentParentCity+currentParentProvince)) != null) {
//          currentChildIds.add(currentNodeId);//join as another child next to current last child of current parent adminDiv
//        } else {
//          currentChildIds = new LinkedHashSet<Long>();
//          currentChildIds.add(currentNodeId);//join as the first child of current parent adminDiv
//          treeFastImage.put(currentParentCity+currentParentProvince, currentChildIds);
//        }
//      }
//    }
//    
//    return null;
//  }

  @Override
  public Page<Channel> findChannels(PageRequest pageRequest) {
    Page<PosLegacy> poses = posLegacyRepo.findAll(pageRequest);
    
    List<Channel> channels = Lists.newArrayList();
    for (PosLegacy posLegacy : poses) {
      PosKindLegacy posKindLegacy = posKindLegacyRepo.findOne(posLegacy.getKindCode());
      Channel channel = new Channel(posLegacy.getCode(), posLegacy.getName(), 
          new ChannelCategory(posKindLegacy.getId(), posKindLegacy.getName()),
          posLegacy.getSysKind(),posLegacy.getAdId());
      channels.add(channel);
    }
    return new PageImpl<Channel>(channels, pageRequest, poses.getTotalElements());
  }

  @Override
  public List<ChannelCategory> findAllChannelCategories() {
    List<PosKindLegacy> posKindLegs = posKindLegacyRepo.findAll();
    List<ChannelCategory> resultNodes = TreeHelper.templateBuildTree(posKindLegs, ChannelCategory.class, posKindLegacyRepo);    
    return resultNodes;
  }

  @Override
  public List<ChannelSysCategory> findAllChannelSysCategories() {
    List<PosSysTypeLegacy> posSysTypes = posSysTypeLegacyRepository.findAll();
    
    List<ChannelSysCategory> channelSysCategories = Lists.newArrayList();
    for (PosSysTypeLegacy posSysTypeLegacy : posSysTypes) {
      PosKindLegacy posKindLegacy = posKindLegacyRepo.findOne(posSysTypeLegacy.getPosKindCode());
      channelSysCategories.add(new ChannelSysCategory(posSysTypeLegacy.getCode(), posSysTypeLegacy.getName(),
          new ChannelCategory(posKindLegacy.getId(), posKindLegacy.getName())));
    }
    return channelSysCategories;
  }

  @Override
  public List<Customer> findAllCustomers() {
    List<CustomerLegacy> customerLegs = customerLegacyRepo.findAll();
    List<Customer> customers = Lists.newArrayList();
    for (CustomerLegacy customerLegacy : customerLegs) {
      customers.add(new Customer(customerLegacy.getCode(), customerLegacy.getName()));
    }
    return customers;
  }
  
  

  @Override
  public List<Product> findAllProductsByCustomerCode(String customerCode) {
    List<BrandLegacy> brandLegs = brandLegacyRepo.findByCustomerCode(customerCode);
    List<Product> products = Lists.newArrayList();
    
    for (BrandLegacy brandLegacy : brandLegs) {      
      List<ProductLegacy> productLegs = productLegacyRepo.findByBrandCode(brandLegacy.getCode());
      for (ProductLegacy productLegacy : productLegs) {
        BrandLegacy brandLeg = brandLegacyRepo.findOne(productLegacy.getBrandCode());
        products.add(new Product(productLegacy.getCode(), productLegacy.getSku(), new Brand(brandLeg.getCode())));
      }
    }
    return products;
  }
}
